// appsplus_jinglian/pages/ticket/ticket.js
var app = getApp();
var province = "广东";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    listtag: [],
    local_city:"深圳市",
    select_area:false,
    select_area_data:['广东','深圳市'],
    provinces:[
      // '北京','河北','安徽','湖北','湖南'
    ],
    citys:[
    ],
    hot_city:[
      "深圳市",
      "上海市",
      "北京市",
      "成都市",
    ],
    show: false,
    pages:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    get_column(this);
    var area = this.data.select_area_data[1].areaname || this.data.local_city 
    get_information(this, area)
    getcitys(this)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pages:1,
    })
    var area = this.data.local_city
    get_information(this, area)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var area = this.data.local_city
    get_information(this, area)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  gotodetail:function(e){
    var tid = e.currentTarget.dataset.id 
    wx.navigateTo({
      url: '../../pages/detail/detail?id='+tid,
    })
  },
  // 搜索跳转
  search: function (e) {
    console.log(e.target.dataset.area)
    wx.navigateTo({
      url: '../../pages/search/search?area=' + e.target.dataset.area,
    })
  },
  // 打开地区选择器
  show_area:function(e){
    console.log(e)
    this.setData({
      // select_area:true,
      areashow:true,
    })
    // get_provinces(this)
  },
  // 取消地区选择
  area_pick_cancel:function(e){
    this.setData({
      select_area: false,
    })
  },
  // 确定地区选择
  area_pick_select:function(e){
    this.setData({
      select_area: false,
    })
  },
  // 监听选择器
  pickChange:function(e){
    console.log(e)
    const val = e.detail.value
    // 选中的省
    var select_province = this.data.provinces[val[0]];

    this.setData({
      select_area_data: [
        this.data.provinces[val[0]],
        this.data.citys[val[1]]
      ]
    })

    if (select_province.areaname == province){
      // 省没变
      get_information(this, this.data.citys[val[1]].areaname)
    }else{
      province = select_province.areaname;
      get_citys(this, select_province.id)
    }
    
    

    
  },
  // 栏目点击
  type_list:function(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    var city = e.currentTarget.dataset.area
    wx.navigateTo({
      url: '../category/category?type=' + id + "&name=" + name + "&city=" + city
    })
  },
  // 城市点击
  cs_city_tap:function(e){
    var that = this;
    console.log(e)
    if(e.currentTarget.dataset.index>0){
      this.setData({
        local_city: e.currentTarget.dataset.cityname,
        areashow:false,
        pages:1
      })
      setTimeout(function(ss){
        get_information(that, e.currentTarget.dataset.cityname)
      },500)
      
    }
  }
})


function get_information(that, area){
  var pages = that.data.pages;
  app.util.request({
    'url': 'entry/wxapp/information',
    data: {
      area: area,
      page: pages
    },
    success: function (res) {
      wx.stopPullDownRefresh()
      console.log(res.data)
      if (res.data.status) {
        var ret = res.data;
        if (pages >1){
          var list = that.data.list;
          list = list.concat(ret.data);
          that.setData({
            list: list
          })
          if(ret.data.length>0){
            that.setData({
              pages: pages + 1,
            })
          }
          if(ret.data.length==0){
            that.setData({
              btm_line: true,
            })
            setTimeout(function () {
              that.setData({
                btm_line: false,
              })
            }, 2000)
            app.util.message("没有更多数据了~")
          }
        }else{
          that.setData({
            list: ret.data,
            show: false
          })
          if (ret.data.length > 0) {
            that.setData({
              pages: pages + 1,
            })
          }
          if (ret.data.length == 0){
            app.util.message("暂无数据~")
          }
        }
      }else{
        that.setData({
          list: [],
          show: true
        })
      }
    }
  });
}

// 获取省级list
function get_provinces(that){
  app.util.request({
    url:'entry/wxapp/area',
    cachetime:'30',
    data:{},
    success: function (res){
      console.log(res)  
      if(res.data.status){
        var ret = res.data;
        that.setData({
          provinces:ret.data,
          
        })
      }
    }
  })
}
// 获取指定省下面的市list
function get_citys(that, parentid){
  app.util.request({
    url: 'entry/wxapp/downtown',
    cachetime: '30',
    data: { parentid: parentid},
    success: function (res) {
      console.log(res)
      if (res.data.status) {
        var ret = res.data;
        that.setData({
          citys: ret.data,
          select_area_data:[
            that.data.select_area_data[0],
            ret.data[0]
          ]
        })
        var area = that.data.select_area_data[1].areaname
        get_information(that, area)
        
      }
    }
  })
}

// 获取栏目类表
function get_column(that){
  app.util.request({
    url: 'entry/wxapp/column_type',
    cachetime: '30',
    data: {  },
    success: function (res) {
      console.log(res)
      if (res.data.status) {
        var ret = res.data;
        that.setData({
          columnlist:ret.data
        })
      }
    }
  })
}
function getcitys (that){
  app.util.request({
    url: 'entry/wxapp/downtown',
    cachetime: '30',
    data: {},
    success: function (res) {
      console.log(res)
      if (res.data.status) {
        var ret = res.data;
        console.log(ret)
        var arr = [];
        var datas = ret.data;
        for (var cs in datas) {
          var csi = datas[cs];
          var arr2 = [cs];
          for(var i = 0;i<datas[cs].length;i++){
            arr2.push(datas[cs][i]);
          }
          arr.push(arr2)
        }
        that.setData({
          
          cs_citysist: arr
        })
      }
    }
  })
}